package com.bw.liuyushan.liuyushan20210904.MVP.Fragment;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bw.liuyushan.liuyushan20210904.Adapter.TableVideoAdapter;
import com.bw.liuyushan.liuyushan20210904.Entity.VideoBean;
import com.bw.liuyushan.liuyushan20210904.MVP.Activity.ItemVideoActivity;
import com.bw.liuyushan.liuyushan20210904.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210904.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210904.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210904.R;
import com.bw.liuyushan.mvp_lib.VIew.BaseFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

/**
 * Liuyushan20210904
 * file name is : Fragment1
 * created by Ender on 2021/9/4 8:25.
 * author : 刘雨杉
 * Describe:
 */
public class FragmentTab1 extends BaseFragment<BasePresenterTest> implements IContrackTest.C_ViewTest, OnRefreshLoadMoreListener {
    private RecyclerView rvVideo;
    int currentPage = 1;
    TableVideoAdapter adapter;
    boolean isAdapter;
    private SmartRefreshLayout smVideo;

    @Override
    public int bindLayout() {
        return R.layout.fragment_tab1;
    }

    @Override
    public void initView() {
        //防止内存泄露
        mPresenter = new BasePresenterTest(new BaseModelTest(), this);

        rvVideo = (RecyclerView) findViewById(R.id.rv_Video);
        smVideo = (SmartRefreshLayout) findViewById(R.id.sm_Video);
        smVideo.setOnLoadMoreListener(this);
        smVideo.setOnRefreshLoadMoreListener(this);
        rvVideo.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
    }

    @Override
    public void initDate() {
        mPresenter.mpVideo("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);
    }

    @Override
    public void ShowVideo(VideoBean videoBean) {
        List<VideoBean.DataBean> data = videoBean.getData();
        initAdapter(data);
    }

    private void initAdapter(List<VideoBean.DataBean> data) {
        smVideo.finishRefresh();
        smVideo.finishLoadMore();
        if (adapter==null){
            adapter=new TableVideoAdapter(data);
            rvVideo.setAdapter(adapter);
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    Intent intent = new Intent(getActivity(), ItemVideoActivity.class);
                    intent.putExtra("object",data.get(position));
                    startActivity(intent);
                }
            });
        }else{
            if (isAdapter){
                adapter.getData().clear();
            }
            adapter.getData().addAll(data);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isAdapter=false;
        currentPage++;
        mPresenter.mpVideo("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isAdapter=true;
        currentPage=1;
        mPresenter.mpVideo("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);
    }
}
